package org.liushengpei.com.service.impl;

import com.alibaba.fastjson2.JSONObject;
import org.liushengpei.com.constant.RedisConstant;
import org.liushengpei.com.constant.ResultMsgConstant;
import org.liushengpei.com.domain.vo.LoginDataVO;
import org.liushengpei.com.mapper.SysUserMapper;
import org.liushengpei.com.service.IAdministrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 账号管理
 */
@Service
public class AdministrationServiceImpl implements IAdministrationService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 获取注册的天数和注册时间
     */
    @Override
    public Map<String, String> getLoginData(String id) {
        Map<String, String> params = new HashMap<>();
        //获取注册时间
        Map<String, String> map = userMapper.getLoginData(id);
        String mapJSON = JSONObject.toJSONString(map);
        LoginDataVO dataVO = JSONObject.parseObject(mapJSON, LoginDataVO.class);
        //日期格式化
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = dataVO.getCreate_time();
        // 获取当前时间戳
        long timestamp = System.currentTimeMillis();
        // 将时间戳转换为Date对象
        Date endDate = new Date(timestamp);
        assert startDate != null;
        LocalDate start = LocalDate.of(startDate.getYear(), startDate.getMonth(), startDate.getDay());
        LocalDate end = LocalDate.of(endDate.getYear(), endDate.getMonth(), endDate.getDay());
        //获取经历的天数
        long days = ChronoUnit.DAYS.between(start, end);
        params.put("experienceDay", String.valueOf(days));
        //注册时间
        String registerDate = dateFormat.format(startDate);
        params.put("registerDate", registerDate);
        //上次登录时间
        String lastLoginTime = dateFormat.format(dataVO.getLogin_time());
        params.put("lastLoginTime", lastLoginTime);
        //登录次数
        params.put("loginNum", String.valueOf(map.get("login_num")));
        return params;
    }

    /**
     * 退出登录
     */
    @Override
    public String exitLogin(String id) {
        int i = userMapper.editLoginStatus(id);
        if (i > 0) {
            //清除缓存token
            redisTemplate.delete(RedisConstant.LOGIN_TOKEN + id);
            //清除用户信息
            redisTemplate.delete(RedisConstant.USER_LOGIN + id);
            return ResultMsgConstant.OK;
        }
        return ResultMsgConstant.FAIL;
    }

    /**
     * 注销账号
     */
    @Override
    public String logOffAccount(String id) {
        int i = userMapper.updateStatusAndDelFlagById(id);
        if (i > 0) {
            //清除缓存token
            redisTemplate.delete(RedisConstant.LOGIN_TOKEN + id);
            //清除用户信息
            redisTemplate.delete(RedisConstant.USER_LOGIN + id);
            return ResultMsgConstant.OK;
        }
        return ResultMsgConstant.FAIL;
    }
}
